#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    Base(long x)
    : _base(x)
    {
        cout << "Base()" << endl;
    }

    virtual void print(){
        cout << "Base::print()" << endl;
    }
    
/* private: */
    long _base;
};

class Derived
: public Base
{
public:
    Derived(long base,long derived)
    : Base(base)
    , _derived(derived)
    {
        cout << "Derived()" << endl;
    }
/* private: */
    long _derived;
};

void test0(){
    Base base(10);
    Derived d1(4,6);

    //底层是base对象调用了Base类的赋值运算符函数
    //base.operator=(d1);
    //形参 const Base & rhs = d1
    base = d1;//ok
    /* d1 = base;//error */

    //派生类向基类转型叫做向上转型
    //基类向派生类转型叫做向下转型
    //
    //直接向上转型是可以的
    Base * pbase = &d1;//ok
    cout << pbase->_base << endl;
    /* cout << pbase->_derived << endl; */

    //直接向下转型都是不行的
    /* Derived * pderived = &base;//error */

    Base & ref = d1;//ok
    cout << ref._base << endl;
    /* cout << ref._derived << endl; */

    /* Derived & ref2 = base;//error */
}

void test1(){
    Base base(6);
    Derived d1(7,9);

    //接下来向下转型不合理
    /* Base * pbase = &base; */

    //合理
    Base * pbase = &d1;
    /* pbase->_derived;//error */

    //此处pbase本身就是指向派生类对象
    //向下转型是合理的
    //但是不能直接向下转型
    /* Derived * pderived = pbase; */

    //如果是合理的向下转型，会返回一个合法的Derived*
    //如果是不合理的向下转型，会返回空指针
    Derived * pd = dynamic_cast<Derived*>(pbase);
    if(pd){
        cout << "转换成功" << endl;
        cout << pd->_derived << endl;
    }else{
        cout << "转换失败" << endl;
    }
}

int main(void){
    test1();
    return 0;
}
